/*
 * Author: yezi
 * E-mail: okye328@gmail.com
 * Create time: Dec 20, 2012 10:05:48 AM
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */

package com.kanbox.linux.token;

import java.net.URI;
import java.net.URISyntaxException;

import org.apache.http.client.utils.URIBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.kanbox.linux.GlobalConf;
import com.kanbox.linux.token.ui.AuthenticationUI;

public class Authenticator {
	private static Logger LOG = LoggerFactory.getLogger(Authenticator.class);
	// sprivate static final String AUTH_URL = "https://auth.kanbox.com/0/auth";

	private static final String SCHEME = "https";
	private static final String HOST = "auth.kanbox.com";
	private static final String PATH = "/0/auth";

	private static final String RESPONSETYPE_KEY = "response_type";
	private static final String RESPONSETYPE_VALUE = "code";

	private static final String CLIENTID_KEY = "client_id";
	private static final String CLIENTID_VALUE = GlobalConf.CLIENT_ID;

	private static final String REDIRECTURI_KEY = "redirect_uri";
	private static final String REDIRECTURI_VALUE = GlobalConf.NATIVE_REDIRECT_URL;

	private static final String PLATFORM_KEY = "user_platform";
	private static final String PLATFORM_VALUE = "linux";

	private static final String LANGUAGE_KEY = "user_language";
	private static final String LANGUAGE_VALUE = "ZH";

	private String authCode;

	public URI buildAuthURI() {
		URIBuilder uriBuilder = new URIBuilder();
		uriBuilder.setScheme(SCHEME).setHost(HOST).setPath(PATH)
				.setParameter(RESPONSETYPE_KEY, RESPONSETYPE_VALUE)
				.setParameter(REDIRECTURI_KEY, REDIRECTURI_VALUE)
				.setParameter(CLIENTID_KEY, CLIENTID_VALUE)
				.setParameter(PLATFORM_KEY, PLATFORM_VALUE)
				.setParameter(LANGUAGE_KEY, LANGUAGE_VALUE);
		try {
			return uriBuilder.build();
		} catch (URISyntaxException e) {
			// Never happen
			LOG.error("Build URI fail!", e);
			return null;
		}
	}

	public Authenticator() {
		String url = buildAuthURI().toString();
		AuthenticationUI au = new AuthenticationUI(this);
		au.setURL(url);
		au.open();
	}

	public static void main(String[] args) {
		Authenticator authenticator = new Authenticator();
		System.out.println(authenticator.getAuthCode());
	}

	public void setAuthCode(String authCode) {
		this.authCode = authCode;
	}

	public String getAuthCode() {
		while (authCode == null) {
		}
		return authCode;
	}
}
